package com.sdk.auth.utils;

import cn.hutool.json.JSONUtil;
import com.sdk.auth.exception.AuthException;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.security.Security;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * @author limaosheng
 * @create 2024-10-15 16:34
 * @desc
 */
@Slf4j
public class AESUtil {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    public static String generateRandomKey(){
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(256, new SecureRandom());
            SecretKey secretKey = keyGenerator.generateKey();
            return Base64.getEncoder().encodeToString(secretKey.getEncoded());
        } catch (Exception e) {
            log.error("AESUtil.generateRandomKey获取随机密钥失败",e);
            throw new AuthException("获取随机密钥失败");
        }
    }

    public static String encrypt(Object data, String key){
        try {
            String str;
            if(data instanceof Integer){
                str = String.valueOf(data);
            }else if(data instanceof Long){
                str = String.valueOf(data);
            }else if(data instanceof Double){
                str = String.valueOf(data);
            }else if(data instanceof Float){
                str = String.valueOf(data);
            }else if(data instanceof BigDecimal){
                str = String.valueOf(data);
            }else if(data instanceof String){
                str = String.valueOf(data);
            }else {
                str = JSONUtil.toJsonStr(data);
            }
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.getDecoder().decode(key), "AES"));
            byte[] encryptedBytes = cipher.doFinal(str.getBytes("UTF-8"));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            log.error("AESUtil.encrypt加密失败：{},data:{}",key,data,e);
            throw new AuthException("加密失败");
        }
    }
    public static String encrypt(String encryptedData){
        String key = generateRandomKey();
        return encrypt(encryptedData,key);
    }


    public static String decrypt(String encryptedData, String key){
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.getDecoder().decode(key), "AES"));
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
            return new String(decryptedBytes,"UTF-8");
        } catch (Exception e) {
            log.error("AESUtil.decrypt解密失败：{},data:{}",key,encryptedData,e);
            throw new AuthException("解密失败");
        }
    }

    public static void main(String[] args){
        String key = generateRandomKey();
        String data = "HellodddddddddWWWWW测试 World!";
        Long id = 1111111L;
        List<Integer> list = new ArrayList<>();
        list.add(1);
        String encryptedData = encrypt(list, key);
        System.out.println("密钥: " + key);
        System.out.println("加密: " + encryptedData);
        String decryptedData = decrypt(encryptedData, key);
        System.out.println("解密: " + decryptedData);
    }
}
